package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.classes.Admin;
import com.example.classes.DataBase;
import com.example.classes.Member;

/**
 * Activity for managing modifying Member accounts.
 * @author      Alexander Huynh <a.huynh @ gatech.edu>
 * @version     1.0
 * @since       2013-03-09
 */
public class ModifyHandler extends Activity
{
    public String username, myname;
    private DataBase db;
    public Member memb;
    private TextView strikes;

    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_screen);

        Intent intent = getIntent();
        username = intent.getStringExtra("selectedname");
        myname = intent.getStringExtra("username");
        Log.v("WMS-DEBUG", "MODIFY: received " + username);

        strikes = (TextView)findViewById(R.id.modify_status);

        db = new DataBase(this);
        db.open();
        try
        {
            memb = db.getEntry(username);

            TextView banner = (TextView) findViewById(R.id.modify_banner);
            banner.setText("Info for: " + username);

            TextView contact = (TextView) findViewById(R.id.modify_contactField);
            contact.setText(memb.getContact());

            TextView memberType = (TextView) findViewById(R.id.modify_type);
            memberType.setText("Type: " + (memb instanceof Admin ? "Admin" : "User"));

            strikes.setText("Strikes: " + memb.getStrikes() + " (" + (memb.isLocked() ? "Locked" : "Unlocked") + ")");
        }
        catch (Exception e)
        {
            Log.v("WMS-DEBUG", "Error: " + e.toString());
        }
    }

    /**
     * Called when the "Unlock account" button is pressed. Unlocks the account by setting strikes to 0.
     * @param view the content view
     */
    public void unlockUser(View view)
    {
        Toast.makeText(ModifyHandler.this, "Strikes reset.", Toast.LENGTH_LONG).show();
        memb.resetStrikes();
        db.update(memb);
        strikes.setText("Strikes: " + memb.getStrikes() + " (" + (memb.isLocked() ? "Locked" : "Unlocked") + ")");
    }

    /**
     * Called when the "Delete account" button is pressed. Deletes the account if appropriate.
     * @param view the content view
     */
    public void deleteUser(View view)
    {
        if (myname.equals(username))
        {
            Toast.makeText(ModifyHandler.this, "Can't delete yourself!", Toast.LENGTH_LONG).show();
            return;
        }
        if (username.equals("admin"))
        {
            Toast.makeText(ModifyHandler.this, "Can't delete default admin!", Toast.LENGTH_LONG).show();
            return;
        }
        db.deleteUserEntry(username);
        Toast.makeText(ModifyHandler.this, "Account deleted.", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * Called when the "Back" button is pressed. Goes back to the home screen.
     * @param view the content view
     */
    public void goBack(View view)
    {
        finish();
    }
}
